<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Scan_admin extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->database();
		$this->load->helper('url');
		$this->load->library('session');
		$this->load->library('table');
		$this->load->helper(array('form', 'url'));
	}
	function scan_cms()
	{
		$scan_name = $this->session->userdata('scan_name');
		if($scan_name!='')
		{
			$data['sign']=$this->session->userdata('scan_name');
			$this->load->model('client_model');	
			$result=$this->client_model->get_category();
			//print_r($result);
			if($result!=FALSE)
			{
				$data['category']=$result;
				$this->load->view('scan_admin/left',$data);
		 		$this->load->view('scan_admin/welcome');
				$this->load->view('scan_admin/right');
			}
			else {
				echo "No Category Records!&nbsp;&nbsp;&nbsp;";
				echo anchor(site_url('welcome/destory'),'退出');
			}	
		}
		else
		{
			$this->load->view('return');
		}	
	}
	function all_books()
	{
		$scan_name = $this->session->userdata('scan_name');
		if($scan_name!='')
		{
			$data['sign']=$this->session->userdata('scan_name');
			$this->load->model('client_model');	
			$result=$this->client_model->get_category();
			//print_r($result);
			if($result!=FALSE)
			{
				$limit=30;
				$this->load->model('manage_books');
				$this->load->library('pagination');
				$config['base_url'] = site_url('scan_admin/all_books');
				$config['total_rows'] = $this->manage_books->get_books_num();
				$config['per_page'] = $limit;
				$this->pagination->initialize($config);
				$offset=$this->uri->segment(3,0);
				$msg['page_result']=$this->manage_books->show_books($config['per_page'],$offset);
				$msg['page_link']=$this->pagination->create_links();
				if($config['total_rows']==1)
				{
					$msg['start']=0;	
				}
				else 
				{
					if($offset==$config['total_rows']-1)
					{
						$msg['start']=$offset-$limit;
					}
					else 
					{
						$msg['start']=$offset;
					}
				}
				$data['category']=$result;
				$this->load->view('scan_admin/left',$data);
		 		$this->load->view('scan_admin/all_books',$msg);
				$this->load->view('scan_admin/right');
			}
			else {
				echo "No Category Records!&nbsp;&nbsp;&nbsp;";
				echo anchor(site_url('welcome/destory'),'退出');
			}	
		}
		else
		{
			$this->load->view('return');
		}	
	}
	function select_category_list($cate_id,$offset='0')
	{
		$scan_name = $this->session->userdata('scan_name');
		if($scan_name!='')
		{
			$data['sign']=$this->session->userdata('scan_name');
			$this->load->model('client_model');	
			$result=$this->client_model->get_category();
			//print_r($result);
			if($result!=FALSE)
			{
				$limit=30;
				$this->load->model('manage_books');
				$msg['cate_name']=$this->manage_books->category_name($cate_id);
				$this->load->library('pagination');	
				$config['base_url'] = site_url('scan_admin/select_category_list'.'/'.$cate_id).'/';
				$config['total_rows'] = $this->manage_books->every_category_nums($cate_id);
				$msg['page_result']=$this->manage_books->every_category_list($limit,$offset,$cate_id);
				$config['uri_segment'] = 4;
				$config['per_page'] = $limit; 
				$this->pagination->initialize($config);
				$msg['cate_id']=$cate_id;	
				if($config['total_rows']==1)
				{
					$msg['start']=0;	
				}
				else 
				{
					if($offset==$config['total_rows']-1)
					{
						$msg['start']=$offset-$limit;
					}
					else 
					{
						$msg['start']=$offset;
					}
				}
				$msg['page_link']=$this->pagination->create_links();
				
				
				$data['category']=$result;
				$this->load->view('scan_admin/left',$data);
		 		$this->load->view('scan_admin/show_list',$msg);	
				$this->load->view('scan_admin/right');
			}
			else {
				echo "No Category Records!&nbsp;&nbsp;&nbsp;";
				echo anchor(site_url('welcome/destory'),'退出');
			}	
		}
		else
		{
			$this->load->view('return');
		}	
	}
	function get_book_message($b_id)
	{
		$scan_name = $this->session->userdata('scan_name');
		if($scan_name!='')
		{
			$data['sign']=$this->session->userdata('scan_name');
			$this->load->model('client_model');	
			$result=$this->client_model->get_category();
			//print_r($result);
			if($result!=FALSE)
			{
				$this->load->model('manage_books');
				$msg=$this->manage_books->get_book_message($b_id);
				$msg['com_num']=$this->manage_books->get_comments_nums($msg['pid']);
				$msg['com']=$this->manage_books->get_ten_comments($msg['pid']);
				$msg['msg']=$msg;
				$data['category']=$result;
				$this->load->view('scan_admin/left',$data);
		 		$this->load->view('scan_admin/display_book',$msg);
				$this->load->view('scan_admin/right');
			}
			else {
				echo "No Category Records!&nbsp;&nbsp;&nbsp;";
				echo anchor(site_url('welcome/destory'),'退出');
			}	
		}
		else
		{
			$this->load->view('return');
		}	
	}
	
	function search_page()
	{
		$key=$this->input->post('key');
		$this->search($key,$offset='0');
	}
	function search($key,$offset='0')
	{
		$scan_name = $this->session->userdata('scan_name');
		if($scan_name!='')
		{
			$data['sign']=$this->session->userdata('scan_name');
			$this->load->model('client_model');	
			$result=$this->client_model->get_category();
			//print_r($result);
			if($result!=FALSE)
			{
				$limit=30;
				$this->load->model('manage_books');
				$this->load->library('pagination');	
				$config['base_url'] = site_url('scan_admin/search'.'/'.$key).'/';
				$config['total_rows'] = $this->manage_books->search_num($key);
				$msg['page_result']=$this->manage_books->search($limit,$offset,$key);
				$config['uri_segment'] = 4;
				$config['per_page'] = $limit; 
				$this->pagination->initialize($config);
				$msg['key']=$key;
				if($config['total_rows']==1)
				{
					$msg['start']=0;	
				}
				else 
				{
					if($offset==$config['total_rows']-1)
					{
						$msg['start']=$offset-$limit;
					}
					else 
					{
						$msg['start']=$offset;
					}
				}
				$msg['page_link']=$this->pagination->create_links();
				$msg['sign']=$this->session->userdata('admin_name');
				$data['category']=$result;
				$this->load->view('scan_admin/left',$data);
		 		$this->load->view('scan_admin/search_result',$msg);
				$this->load->view('scan_admin/right');
			}
			else {
				echo "No Category Records!&nbsp;&nbsp;&nbsp;";
				echo anchor(site_url('welcome/destory'),'退出');
			}	
		}
		else
		{
			$this->load->view('return');
		}	
	}
}























